Vue.filter('handleTime', (time)=>{
    return time1(time)
    function time1(t) {
        let s  = t.slice(0, 10)
        let year = Number(s.split('/')[0]) 
        let month = Number(s.split('/')[1])
        let day = Number(s.split('/')[2])
        //alert(year+" "+month+" "+day)
        let time1 = getDays(year, month, day) // 目标时间
        let time2 = getDays(vm.now.year, vm.now.month, vm.now.day) //参照时间
        if (year === vm.now.year) {//本年
            if (vm.now.year === year && vm.now.month === month && vm.now.day === day){ //当天
                return t.slice(11, 16) //只返回 时:分
            }else if (vm.now.year === year && time2 - time1 === 1) { //昨天(仅处理同年同月场景)
                return `昨天${t.slice(11, 16)}`  //返回 昨天+时:分
            }else if (vm.now.year === year && time2 - time1 > 1) {
                return t.slice(5, 16) //只返回 月/日 时:分
            }else{ //昨天以前
                return t.slice(5, 16) //只返回 月/日 时:分
            }
        }else{//往年
            return t.slice(0, 16) //返回完整时间 年/月/日 时:分
        } 
    }/**/
    function isfullyear(year){
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        }
    }
    function getDays(year, month, day){
        //把天数赋值给num
        var num = day;
        //如果用户输入1月 则直接返回天数
        if (month == 1) {
            return day;
        }
        var monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        //循环遍历月份 循环条件是用户输入月份的前一个月份 
        for (var i = 0; i < month - 1; i++) {
            //num = 当月天数 + 之前每个月的天数
            num = num + monthDay[i]
        }
        //如果用户输入的年份是闰年并且输入的月份大于2 那么总天数加1
        if (isfullyear(year) && month > 2) {
            num++;
        }
        return num;
    }
})
var vm = new Vue({
    el: "section",
    data: {
        now: {},
        GroupID: "",
        GroupName: "",
        GroupMembers: 0,
        messageList: [],
        senderList: [],
        contentList: [
        ],
        userMsg: "",
        dis: false,
        alertShow: false,
        action1Show: false,
        alt_title: "提示",
        alt_body: ""
        
    },
    computed: {
        myStringByteCount: function() {  
            return this.getByteCount1(this.myString);  
        } 
    },
    methods: {
        getByteCount1: function(str) {  
            let count = 0;  
            for (let i = 0; i < str.length; i++) {  
                count += str.charCodeAt(i) > 127 ? 2 : 1;  
            }  
            return count;  
        },
        getByteCount(str) {  
            let count = 0;  
            for (let i = 0; i < str.length; i++) {  
                count += str.charCodeAt(i) > 127 ? 2 : 1;  
            }  
            return count;  
        },
        checkInput(){
            
        },
        send(){
            if(/^\s*$/.test(this.userMsg)){
                this.alertShow = true
                this.alt_body = "不能发送空白消息"
                this.userMsg = ""
            }else{
                this.dis = true
                let userInfo = localStorage.getItem('XGB-user')
                let userid = encodeURIComponent(JSON.parse(userInfo).id) 
                this.contentList.push({
                    "role": "me",
                    user: {
                        id: userid,
                        nickname: JSON.parse(userInfo).name,
                        img: JSON.parse(userInfo).userImg
                    },
                    msg: {
                        id: "",
                        content: this.userMsg,
                        time: "2024/04/08 15:15:50"
                    }
                })
                let time1 = setTimeout(()=>{
                    document.querySelector('.chatUl li:last-child .van-loading').style.display='flex'
                    document.querySelector('.chatUl li:last-child').scrollIntoView({
                        behavior: 'smooth'
                    })
                    this.userMsg = ""
                    clearTimeout(time1)

                }, 10)
                //发送请求
                // let userMsg = encodeURIComponent(this.userMsg)

                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
                let method = `sendGroupMessage_E54F534efg53e4G5`
                let data = {
                    userid: userid,
                    GroupID: this.GroupID,
                    content: this.userMsg
                }
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    if(res.data.code !== 0){
                        this.alertShow = true
                        this.alt_body = res.data.msg+" ["+res.data.code+"]"
                    }else{
                        console.log(res.data)
                        document.querySelector('.chatUl li:last-child .van-loading').style.display='none'
                    }
                })
                //处理完毕
  
            }
            
            
        },
        isShowTime(time, index){
            let ord = index+1
            let isShowTime = false
            if(ord == this.contentList.length){
                isShowTime = true
            }else if(ord % 3 == 0){
                isShowTime = true
            }else if(ord == 1){
                isShowTime = true
            }
            return isShowTime
        },
        cancel() {
            if(this.code !== 0){
                this.alertShow = false
            }else{
                this.alertShow = false
                window.location.replace('../../login/login.html')
            }
            
        },
        yes() {
            console.log("用户点击 “确定”")
        }
    },
    mounted() {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth()+1
        let day = date.getDate()
        this.now = {
            year: year,
            month: month,
            day: day
        }
        let params1 = location.search.substring(1)
        this.GroupID = params1.split('&')[0].split('=')[1]
        this.GroupName = decodeURI(params1.split('&')[1].split('=')[1])
        let hadNewMessage = (params1.split('&')[2].split('=')[1] === "true") ? true: false
        
        // 获取消息
        let userInfo = localStorage.getItem('XGB-user')
        let userid = encodeURIComponent(JSON.parse(userInfo).id) 
        // let userMsg = encodeURIComponent(this.userMsg)

        let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
        let method = `getGroupMessage_68SDB415N6YHF8n4br`
        let data = {
            userid: userid,
            GroupID: this.GroupID,
            hadNewMessage: hadNewMessage
        }
        axios.post(`${url}/${method}`, data)
        .then(res=>{
            if(res.data.code === 0){
                //接收到消息
                console.log(res.data)
                let messageList = res.data.data.messageList
                
                let senderList = res.data.data.senderList
                this.GroupMembers = res.data.data.member
                document.querySelector("header .title").innerText = `${this.GroupName}(${this.GroupMembers})`
                messageList.forEach((item, index) => {
                    senderList.forEach((item2, index2)=>{
                        if(item.SenderID == item2._id){
                            this.contentList.unshift({
                                role: (userid == item2._id) ? "me" : "others",
                                user: {
                                    id: item2._id,
                                    nickname: item2.name,
                                    img: item2.userImg
                                },
                                msg: {
                                    id: item._id,
                                    content: item.Content,
                                    time: item.time
                                }
                            })
                            return //终止内层循环
                        }


                    })
                });
                setTimeout(()=>{
                    document.querySelector('.chatUl li:last-child').scrollIntoView({
                        behavior: 'smooth'
                    })
                },1)


            }else{

            }
        })
    }
})